//
//  JBVisitorView.swift
//  sinaBlog
//
//  Created by jobo on 15/10/28.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

protocol JBVisitorVeiwDelegate : NSObjectProtocol
{
    func reginClickDel()
    
    func loginClickDel()
}

class JBVisitorView: UIView {

    weak var visitorViewDelegate : JBVisitorVeiwDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepraforUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置不同control的控件
    func setVisitorView( iconImgName:String , label:String )
    {
        
        homeImgView.hidden = true
        
        iconImgView.image = UIImage(named: iconImgName)
        
        textLable.text = label ;
        
        sendSubviewToBack(coverImgView)
        
        
    
    }
    
    func reginClick()
    {
        print("注册")
        
        visitorViewDelegate?.reginClickDel()
    }
    
    func loginClick()
    {
        print("登录")
        
        visitorViewDelegate?.loginClickDel()
    }
    
    
    //设置转轮动画
    func startAnmation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        iconImgView.layer.addAnimation(animation, forKey: nil)
        
    }
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconImgView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconImgView.layer.speed = 0
        
        // 设置动画偏移时间
        iconImgView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconImgView.layer.timeOffset
        
        // 设置动画速度为1
        iconImgView.layer.speed = 1
        
        iconImgView.layer.timeOffset = 0
        
        iconImgView.layer.beginTime = 0
        
        let timeSincePause = iconImgView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconImgView.layer.beginTime = timeSincePause
    }

    
    
    //准备ui添加视图控件及控件约束
    func prepraforUI()
    {
        addSubview(iconImgView)
        addSubview(coverImgView)
        addSubview(homeImgView)
        addSubview(textLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        iconImgView.translatesAutoresizingMaskIntoConstraints = false
        coverImgView.translatesAutoresizingMaskIntoConstraints = false
        homeImgView.translatesAutoresizingMaskIntoConstraints = false
        textLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 创建约束
        // view1.attr1 = view2.attr2 * multiplier + constant
        
        // 转轮
        // CenterX
        self.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // CenterY
        self.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //遮罩
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 小房子
        // x
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImgView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label
        // x
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // y
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // width
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // 注册按钮
        // 左边
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 顶部
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右边
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 顶部
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

        
    }
    
    //各种懒加载
    lazy var iconImgView : UIImageView =
    {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        imgView.sizeToFit()
        
        return imgView
    
    }()
    
    
    lazy var coverImgView : UIImageView =
    {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
       // var bounds = UIScreen.mainScreen().bounds
        
        //imgView.frame = CGRectMake(0, 0, bounds.width, bounds.height)
        
        return imgView
    }()

    
    lazy var homeImgView : UIImageView =
    {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        imgView.sizeToFit()
        
        return imgView
    
    }()
    
    lazy var textLable : UILabel =
    {
        let label = UILabel()
        
        label.text =  "关注一些人,看看有什么惊喜"
        
        label.textAlignment = NSTextAlignment.Center
        
        label.textColor = UIColor.lightGrayColor()
        
        label.numberOfLines = 0
        
        label.sizeToFit()
        
        return label

    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        //注册按钮点击事件
        
        button.addTarget(self, action: "reginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        //登录按钮点击事件
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    
}
